package pl.vhcreator.domain;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Collections;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class HostWriter 
{
    private String pathToHost = "/etc/hosts";
    
    private Pattern pattern = Pattern.compile("127\\.0\\.([0-9]{0,3})\\.[0-9]{0,3}(.*)");    
    
    private ArrayList<String> messages = new ArrayList<String>();
    
    private String address = "127.0.0.1";
    
    private String hostName = null;
    
    public void addMessage(String msg) 
    {
        this.messages.add(msg);
    }
    
    public void setMessage(String msg) 
    {
        this.messages = new ArrayList<String>();
        this.messages.add(msg);
    }
    
    public List<String> getMessage() 
    {
        return messages;
    }
    
    public void setPathToHost(String pathToHost) 
    {
        this.pathToHost = pathToHost;
    }
    
    public String getPathToHost() 
    {
        return this.pathToHost;
    }
    
    public void setHostName(String hostName) 
    {
        this.hostName = hostName;
    }
    
    public String getHostName()
    {
        return hostName;
    }
     
    public boolean writeNewAdress()
    {
        if (null == hostName) {
            addMessage("Host name (URL) is required.");
            return false;
        }
        
        File isFile = new File(getPathToHost());
        if (! isFile.exists()) {
            addMessage("Hosts file don't exist !");
            return false;
        }
        
        setAddress();
        try { 
            FileWriter fileWriter = new FileWriter(getPathToHost(), true);
            
            
            String template = address + "\t" + hostName;
            addMessage("Add new host IP address: " + address + " " + hostName);
            fileWriter.append(template);
            fileWriter.close();
            
        } catch (IOException e) {
            addMessage(e.getMessage());
            return false;
        }
        return true;        
    }
    
    public String getAddress() 
    {
        return address;
    }
    
    private void setAddress() 
    {
        FileReader fileReader = null;
        ArrayList<String> lines = new ArrayList<String>();
        
        try { 
            fileReader = new FileReader(getPathToHost());
            BufferedReader buffer = new BufferedReader(fileReader);
            
            String line;
            while ((line = buffer.readLine()) != null) {
                lines.add(line);
            }
            buffer.close();
            
        } catch (IOException e) {
            addMessage(e.getMessage());
        }
        
        if (lines.size() > 0) {
            Collections.reverse(lines);
            int addressNo = 0;
            for (String line : lines) {
                if ((addressNo = parseLine(line)) != 0) {
                    address = "127.0." + addressNo + ".1";
                    addMessage("New address: " + address);
                    break;
                }
            }
        }        
    }
    
    private int parseLine(String line)
    {
        int addressNo = 0;
        Matcher matcher = pattern.matcher(line);
        if (matcher.matches()) {
            addressNo = Integer.parseInt(matcher.group(1)) + 1;
        }
        return addressNo;
    }
    
    
    
}